package com.huaxin.hxmodulesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaxin.hxmodulesystem.dto.ResponseCode;
import com.huaxin.hxmodulesystem.dto.ResponseResult;
import com.huaxin.hxmodulesystem.dto.request.AddSysDictionaryReqDTO;
import com.huaxin.hxmodulesystem.dto.request.UpdateSysDictionaryReqDTO;
import com.huaxin.hxmodulesystem.mapper.SysDictionaryMapper;
import com.huaxin.hxmodulesystem.mapping.SysDictionaryMapping;
import com.huaxin.hxmodulesystem.models.SysDictionary;
import com.huaxin.hxmodulesystem.service.SysDictionaryService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class SysDictionaryServiceImpl extends ServiceImpl<SysDictionaryMapper, SysDictionary> implements SysDictionaryService {
    private final SysDictionaryMapping mapping;

    private final CacheManager cacheManager;

    private final ApplicationContext applicationContext;

    //添加字典
    @Override
    public ResponseResult<Boolean> addSystemDict(AddSysDictionaryReqDTO in) {
        if (in == null) {
            return ResponseResult.parameterError(null, "参数错误！");
        }
        if (StringUtils.isBlank(in.getDictType())) {
            return ResponseResult.parameterError(null, "字典类型不能为空！");
        }
        if (StringUtils.isBlank(in.getDictKey())) {
            return ResponseResult.parameterError(null, "字典键不能为空！");
        }
        if (StringUtils.isBlank(in.getDictValue())) {
            return ResponseResult.parameterError(null, "字典值不能为空！");
        }
        if (this.baseMapper.selectCount(new QueryWrapper<SysDictionary>().eq("dict_key", in.getDictKey())) > 0) {
            return ResponseResult.parameterError(null, "字典键已存在！");
        }
        SysDictionary systemDict = mapping.toEntity(in);
        return this.baseMapper.insert(systemDict) > 0 ? ResponseResult.ok(null) : ResponseResult.failure(ResponseCode.INTERNAL_ERROR, "添加字典失败！", null);
    }

    //修改字典
    @Override
    @CacheEvict(value = "dictionaryInfo", key = "#in.dictKey")
    public ResponseResult<Boolean> updateSystemDict(UpdateSysDictionaryReqDTO in) {
        if (in == null) {
            return ResponseResult.parameterError(null, "参数错误！");
        }
        if (StringUtils.isBlank(in.getDictKey())) {
            return ResponseResult.parameterError(null, "字典Key不能为空！");
        }
        if (StringUtils.isBlank(in.getDictValue())) {
            return ResponseResult.parameterError(null, "字典值不能为空！");
        }
        SysDictionary systemDict = this.baseMapper.selectOne(new QueryWrapper<SysDictionary>().eq("dict_key", in.getDictKey()));
        if (systemDict == null) {
            return ResponseResult.failure(ResponseCode.INTERNAL_ERROR, "字典不存在", false);
        }
        mapping.updateEntity(in, systemDict);
        return this.baseMapper.updateById(systemDict) > 0 ? ResponseResult.ok(null) : ResponseResult.failure(ResponseCode.INTERNAL_ERROR, "修改字典失败！", false);
    }

    //删除字典
    @Override
    @CacheEvict(value = "dictionaryInfo", key = "#dictKey")
    public ResponseResult<Boolean> deleteSystemDict(String dictKey) {
        SysDictionary systemDict = this.baseMapper.selectOne(new QueryWrapper<SysDictionary>().eq("dict_key", dictKey));
        if (systemDict == null) {
            return ResponseResult.failure(ResponseCode.INTERNAL_ERROR, "字典不存在", null);
        }
        if (applicationContext.getBean(SysDictionaryService.class).removeById(systemDict.getId())) {
            return ResponseResult.ok(true);
        } else {
            return ResponseResult.failure(ResponseCode.INTERNAL_ERROR, "删除字典失败！", null);
        }
    }

    //查询字典
    @Override
    public ResponseResult<List<SysDictionary>> querySystemDict(String dictType) {
        QueryWrapper<SysDictionary> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(dictType)) {
            queryWrapper.eq("dict_type", dictType);
        }
        return ResponseResult.ok(this.baseMapper.selectList(queryWrapper));
    }

    //根据key查询字典数据
    @Override
    @Cacheable(value = "dictionaryInfo", key = "#dictKey", unless = "#result == null")
    public SysDictionary querySystemDictByKey(String dictKey) {
        QueryWrapper<SysDictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_key", dictKey);
        queryWrapper.eq("is_active", 1);
        SysDictionary systemDict = this.baseMapper.selectOne(queryWrapper);
        return systemDict;
    }
}